﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers.Article
{
    public class Comment_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Article/comment_edit.cshtml";
        private int id = 0;
        protected Article_comment model = new Article_comment();
        public IArticle_commentService _article_comment { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ActionResult result = View(EDIT_RESULT_VIEW);
            int.TryParse(Request.Query["id"], out this.id);
            if (id == 0)
            {
                JscriptMsg("传输参数不正确！", "back");
                filterContext.Result = result;
                return;
            }
            if (!_article_comment.Exists(this.id))
            {
                JscriptMsg("记录不存在或已删除！", "back");
                filterContext.Result = result;
                return;
            }
            ViewBag.Id = this.id.ToString();
        }
        //
        // GET: /admin/Comment_Edit/

        public ActionResult Index()
        {
            ShowInfo();
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ChkAdminLevel("article_" + model.channel_id + "_comment", CMSEnums.ActionEnum.Reply.ToString()); //检查权限
            model.is_reply = 1;
            model.reply_content = Utils.ToHtml(Request.Form["txtReContent"]);
            model.is_lock = int.Parse(Request.Form["rblIsLock"]);
            model.reply_time = DateTime.Now;
            _article_comment.Update(model);
            AddAdminLog(CMSEnums.ActionEnum.Reply.ToString(), "回复评论ID:" + model.id); //记录日志
            string result=JscriptMsg("评论回复成功！", "../comment_list/index?channel_id=" + model.channel_id);
            return Content(result);
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            this.model = _article_comment.GetModel(this.id); //取得评论实体
            ViewData["model"] = this.model;
        }
        #endregion
    }
}
